<?php
include '../include/functions.php';
dbConnect();
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$start = time();
$sessionId = session_id();
if(!$userID)
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}

require_once('../include/processPick.php');
require_once('../include/Pick.php');
//check to see if a Weeknum value was passed, if not, set WeekNum to Weeknum.
if (($Weeknum == NULL) || ($Weeknum == "")) {
	$Weeknum = getWeek("NHL");
}

//check to make sure $Weeknum > 1 AND $Weeknum < 21
$Weeknum = checkWeekBounds($Weeknum);
$ListPicks = getWeighedList($userID,$Weeknum,"NHL");
$UnwieghablePicks = getUnWeighedList($userID,$Weeknum,"NHL");
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
  <title>JoshHughes.com NHL Hockey Pool</title>
	<link rel="shortcut icon" href="http://hockey.joshhughes.com/NHL/images/nhlFavicon.gif" />
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/NHL.css" type="text/css">
	<script src="http://jquery.com/src/latest/"></script>
	<script type="text/javascript" src="http://hockey.joshhughes.com/scriptaculous-js-1.7.0/lib/prototype.js"></script>
    <script type="text/javascript" src="http://hockey.joshhughes.com/scriptaculous-js-1.7.0/src/scriptaculous.js"></script>
    <script type="text/javascript" src="http://hockey.joshhughes.com/scriptaculous-js-1.7.0/src/effects.js"></script>
    <script type="text/javascript" src="http://hockey.joshhughes.com/scriptaculous-js-1.7.0/src/dragdrop.js"></script>
    <script type="text/javascript" src="http://hockey.joshhughes.com/scriptaculous-js-1.7.0/src/controls.js"></script>
	<script src="http://hockey.joshhughes.com/scripts/hockey.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/weighpicks.js" type="text/javascript"></script>
    
</head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<table width="550" cellpadding="5" cellspacing="0">
				<tr>
					<td align="center" height="1"><h2>Weighted Picks</h2></td>
				</tr>
				<tr>
					<td align="center" height="1">Also known as Confidence picks.  Simply drag &amp; drop your picks to order them from high (most confident of) to low (least confident of).
					  The point value for that pick will be shown on the left.
					    If you get that pick correct, you get that many points. See more on weighted picks on the <a href="file:///Macintosh%20HD/Users/josh/Sites/rules.php">rules & FAQs page</a>.</td>
				</tr>
			</table>
			<table width="550" cellpadding="5" cellspacing="0">
				<tr>
				  <td class="bluebg">
					  <table bgcolor=#FFFFFF width="600" align="center">
						<tr>
							<td colspan="7" align="center"><H1><?	getWeekBanner($Weeknum)	?></H1></td>
							<td align="right"><a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum-1; ?>" class="permLink">Prev</a> | <a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum+1; ?>" class="permLink">Next</a> &nbsp; or &nbsp; 
								<B>Go to:</B>
								<form action="<? echo $PHP_SELF; ?>" method="POST" name="WeekForm">
								<?
									WeekDropDown($Weeknum)
								?>
								</form>
							</td>
						</tr>
					  </table>
					  <table bgcolor=#FFFFFF width="600" align="center">
						<tr>
						<td>
						<?
							//if(($Weeknum > 17) && ($MadePlayoffs == 1))
							if ($Weeknum < 17)
							{
								if ($ListPicks || $UnwieghablePicks)
								{
									if ($ListPicks)
									{									
										echo "<form action=\"submitpicks.php\" name=\"PicksForm\" id=\"PicksForm\" method=\"POST\">
										<ul class=\"sortablelist\" id=\"pick_list\">";
										foreach ($ListPicks as $Pick)
										{
											echo "<li id=\"pick_".$Pick->GameNum."\">"; 
											$Pick->Display("NHL");
											echo "</li>\n";
										}
										echo "</ul>
										<table align=\"center\"><tr><td><input type=\"hidden\" name=\"UserID\" value=\"$userID\">
											<input type=\"hidden\" name=\"Week\" value=\"$Weeknum\">
											<input type=\"reset\" value=\"Reset Form\"> | <input type=\"Submit\" name=\"Submit\" value=\"Update Picks\"></td></tr></table>
										</form>";
										?>						
											<script type="text/javascript">
											
												function updateOrder()
												{
													// turn on update message here
													//new Effect.Highlight('text',{startcolor:'#cccccc', endcolor:'#0066cc', restorecolor:'#0066cc', duration: 1.5});
													var options = {
																	method : 'post',
																	parameters : Sortable.serialize('pick_list'),
																	asynchronous: true,
																	onComplete : function(request){
																		// turn off update message here
																		//new Effect.Highlight('text',{startcolor:'#0066cc', endcolor:'#cccccc', restorecolor:'#cccccc', duration: 2.0});
																	}
																  };
												 
													new Ajax.Request('../include/processor.php?func=<?=$userID?>&w=<?=$Weeknum?>&L=NHL', options);
												}
												Sortable.create('pick_list', { onUpdate : updateOrder });
											</script>				
										<?
									}					
									if($UnwieghablePicks)
									{
										$WeeklyScore = 0;
										echo "<hr/>Games that are already in progress or over.  (I.E. cannot be wieghed now)<hr/>";
										echo "<ul class=\"unsortablelist\" id=\"unweighable_list\">";
										foreach ($UnwieghablePicks as $Pick)
										{
											echo "<li id=\"unweighablepick_".$Pick->GameNum."\">"; 
											$Pick->Display("NHL");
											echo "</li>\n";
											if ($Pick->UserPick == $Pick->Winner)
												$WeeklyScore += $Pick->Ranking;
										}
										echo "</ul>";
									}
									echo "<h3 align=\"center\">Week $Weeknum Weighted score: <span style=\"color: #FF0000;\">$WeeklyScore</span></h3>";
								}
								else
								{
									// Make An entry in NHLPicks for this user/week if there isn't one.
									$myrow = mysql_fetch_array(mysql_query("SELECT UserID FROM NHLPicks WHERE UserID=".$userID." AND Week=".$Weeknum));
									if (($myrow["UserID"] == NULL) || ($myrow["UserID"] == ""))
										mysql_query("INSERT INTO NHLPicks (UserID, Week) VALUES($userID,$Weeknum)") or die("Cannot query the database.<br/>");
										
									// Make An entry in NHLWeighPicks for this user/week if there isn't one.
									$myrow = mysql_fetch_array(mysql_query("SELECT UserID FROM NHLWeighPicks WHERE UserID=".$userID." AND Week=".$Weeknum));
									if (($myrow["UserID"] == NULL) || ($myrow["UserID"] == ""))
										mysql_query("INSERT INTO NHLWeighPicks (UserID, Week) VALUES($userID,$Weeknum)") or die("Cannot query the database.<br/>");
									?>
									<SCRIPT language="JavaScript">
									<!--
									parent.location="<? echo $PHP_SELF."?Weeknum=$Weeknum"; ?>";
									//-->
									</SCRIPT>
									<?
								}
							} //END if(($Weeknum > 17) && ($MadePlayoffs == 1))
							else
							{
								?>
								<table bgcolor="#FFFFFF" border="0" width="100%">
									<tr>
										<td align="center">I'm sorry, you didn't make the playoffs.  You can log in to the site, but cannot make any more picks.  If you think there has been a mistake please contact the administrator though the suggestion box (in bottom left corner).</td>
									</tr>
								</table>
								<?
							}
						?>
						</td>
						</tr>
					  </table>
				  </td>
				</tr>
			  </table>
			
			</div>

			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
  </body>
</html>